/**
 * 
 */
package com.archerdu.apply.service.impl;

import java.io.PrintWriter;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.archerdu.apply.dao.ProductCatalogDAO;
import com.archerdu.apply.dao.ProductDAO;
import com.archerdu.apply.model.Product;
import com.archerdu.apply.model.ProductCatalog;
import com.archerdu.apply.service.ProductCatalogManager;
import com.archerdu.base.dao.IBaseDao;
import com.archerdu.base.dao.TreeNode;
import com.archerdu.base.exception.BussinessException;
import com.archerdu.base.service.BaseManager;
import com.archerdu.commons.constant.ErrorConstant;
import com.archerdu.commons.util.XMLUtil;

/**
 * @author archer
 * @version 1.0.0
 */
public class ProductCatalogManagerImpl extends BaseManager<ProductCatalog, String> implements
        ProductCatalogManager {

    private static final long serialVersionUID = 4966611154968220095L;

    private ProductCatalogDAO ProductCatalogDao;
    private ProductDAO productDao;

    @Override
    public void delete(ProductCatalog ProductCate) throws Exception {
        log.info("开始删除");
        if (null != ProductCate && null != ProductCate.getId()) {
            ProductCate = ProductCatalogDao.get(ProductCate.getId());
            if (null != ProductCate) {
                Product product = new Product();
                product.setCatalogId(ProductCate.getId());
                // 目录下有内容， 不能删除
                if (productDao.pageSearch(product, 1, 2).getTotal() != 0) {
                    throw new BussinessException(ErrorConstant.CATE_EXISTS_CONTENT);
                }
                // 有子目录， 不能删除
                if (ProductCatalogDao.findByParentId(ProductCate.getId()).size() != 0) {
                    throw new BussinessException(ErrorConstant.CATE_EXISTS_CHILD);
                }
                try {
                    log.info("执行删除");
                    super.delete(ProductCate);
                }
                catch (Exception e) {
                    log.error(e.getMessage(), e);
                    throw new BussinessException(ErrorConstant.DELETE_ERROR);
                }
            }
            else {
                throw new BussinessException(ErrorConstant.CATE_NOT_EXISTS);
            }
        }
        else {
            throw new BussinessException(ErrorConstant.CATE_NOT_EXISTS);
        }
    }

    public ProductCatalogDAO getProductCatalogDao() {
        return ProductCatalogDao;
    }

    public void setProductCatalogDao(ProductCatalogDAO dao) {
        ProductCatalogDao = dao;
    }

    @Override
    protected IBaseDao<ProductCatalog, String> getDao() {
        return (IBaseDao<ProductCatalog, String>) ProductCatalogDao;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.archer.manager.ProductCateManager#toXML(java.io.OutputStream)
     */
    public void toXML(PrintWriter pw) {
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement(ProductCatalog.ROOT);
        root.addAttribute("id", "0");
        ProductCatalog ProductCate = new ProductCatalog();
        ProductCate.setId("0");
        loadChildren(root, ProductCate);

        XMLUtil.save(pw, doc);
    }

    @Override
    public void add(ProductCatalog entity) throws Exception {
        // 上级目录不存在
        if (entity.getParentId() == null) {
            throw new BussinessException(ErrorConstant.CATE_PARENT_NOT_EXISTS);
        }
        if (entity.getParentId().equals("0")) {
            entity.setCode("");
            entity.setLev(0);
        } else {
            ProductCatalog ProductCate = ProductCatalogDao.get(entity.getParentId());
            entity.setCode(ProductCate.getCode() + ProductCate.getId());
            entity.setLev(ProductCate.getLev() + 1);
        }
        super.add(entity);
    }

    public void loadChildren(Element ele, ProductCatalog ProductCate) {
        List<ProductCatalog> list = getProductCatalogDao().findByParentId(ProductCate.getId());
        if (list.size() > 0) {
            for (ProductCatalog ca : list) {
                Element el = ele.addElement(TreeNode.ITEM);
                ca.toElement(el);
                loadChildren(el, ca);
            }
        } else {
            return;
        }
    }

    public ProductDAO getProductDao() {
        return productDao;
    }

    public void setProductDao(ProductDAO productDao) {
        this.productDao = productDao;
    }
}
